#tag Window
Begin Window Window1
   BackColor       =   16777215
   Backdrop        =   ""
   CloseButton     =   True
   Composite       =   False
   Frame           =   0
   FullScreen      =   False
   HasBackColor    =   False
   Height          =   432
   ImplicitInstance=   True
   LiveResize      =   True
   MacProcID       =   0
   MaxHeight       =   32000
   MaximizeButton  =   False
   MaxWidth        =   32000
   MenuBar         =   430950399
   MenuBarVisible  =   True
   MinHeight       =   64
   MinimizeButton  =   True
   MinWidth        =   64
   Placement       =   0
   Resizeable      =   True
   Title           =   "Untitled"
   Visible         =   True
   Width           =   600
   Begin PushButton btnGenerate
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "Generate"
      Default         =   ""
      Enabled         =   True
      Height          =   29
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   500
      LockBottom      =   True
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   True
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   0
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      Top             =   390
      Underline       =   ""
      Visible         =   True
      Width           =   80
   End
   Begin TextArea taCode
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   16777215
      Bold            =   ""
      Border          =   True
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   364
      HelpTag         =   ""
      HideSelection   =   True
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LimitText       =   0
      LockBottom      =   True
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   True
      LockTop         =   True
      Mask            =   ""
      Multiline       =   True
      ReadOnly        =   ""
      Scope           =   0
      ScrollbarHorizontal=   ""
      ScrollbarVertical=   True
      Styled          =   ""
      TabIndex        =   1
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   15
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   560
   End
   Begin TextField tfPlainText
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   16777215
      Bold            =   ""
      Border          =   True
      CueText         =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LimitText       =   0
      LockBottom      =   True
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   True
      LockTop         =   ""
      Mask            =   ""
      Password        =   ""
      ReadOnly        =   ""
      Scope           =   0
      TabIndex        =   2
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Hello World"
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   0
      Top             =   393
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   392
   End
   Begin CheckBox chkAddcaller
      AutoDeactivate  =   True
      Bold            =   ""
      Caption         =   "Caller"
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   418
      LockBottom      =   True
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   True
      LockTop         =   ""
      Scope           =   0
      State           =   0
      TabIndex        =   3
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      Top             =   395
      Underline       =   ""
      Value           =   False
      Visible         =   True
      Width           =   78
   End
End
#tag EndWindow

#tag WindowCode
	#tag Method, Flags = &h0
		Function generator(inText As String, ByRef functionName As String) As String
		  dim b64 as String = EncodeBase64( inText,0 )
		  dim b64len As String = Format( b64.Len , "0" )
		  dim arry() As String = Split( b64, "" )
		  dim ctr() As Integer
		  for i As Integer = 0 to UBound( arry )
		    ctr.Append i
		  next
		  ctr.Shuffle
		  
		  functionName = randomIdentifier
		  dim mb As String = randomIdentifier( 4, 7 )
		  dim code() As String
		  code.Append "Function " + functionName + "() As String"
		  code.Append "  Dim " + mb + " As new MemoryBlock( "+ b64len +" )"
		  for i as Integer = 0 to UBound( arry )
		    dim num As Integer = ctr( i )
		    code.Append "  " + mb + ".UInt8Value(" + _
		     format( num ,"0") + _
		    ")= " + format( asc( arry( num ) ), "0" )
		  next
		  code.Append "  Return DecodeBase64( " + mb + ".StringValue( 0, " + b64len +" ) )"
		  code.Append "End Function"
		  
		  Return join( code, EndOfLine )
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function randomIdentifier(minLength As Integer = 6, maxLength As Integer = 10) As String
		  dim chars As String = _
		  "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		  static r As new Random
		  dim pos As Integer = r.InRange( 1, 26 )
		  dim retVal as String = Mid( chars, pos, 1 )
		  dim maxI As Integer = r.InRange( minLength-1, maxLength-1 )
		  for i as Integer = 1 to maxI
		    pos = r.InRange( 1, 62 )
		    retVal = retVal + Mid( chars, pos, 1 )
		  next
		  
		  Return retVal
		End Function
	#tag EndMethod


#tag EndWindowCode

#tag Events btnGenerate
	#tag Event
		Sub Action()
		  dim fName As String
		  taCode.Text = generator( tfPlainText.Text, fName )
		  if chkAddcaller.Value Then
		    taCode.Text = taCode.Text + EndOfLine + EndOfLine + _
		    "Sub Caller() " +EndOfLine + _
		    "  Dim s as String = " + fName + "()" + EndOfLine + _
		    "End Sub"
		  end if
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events taCode
	#tag Event
		Function ConstructContextualMenu(base as MenuItem, x as Integer, y as Integer) As Boolean
		  dim m As MenuItem
		  base.Append EditMenu
		  m = new MenuItem( "CopyBefore")
		  if me.SelStart <= 0 then
		    m.Enabled = False
		  end if
		  base.Append m
		  m = new MenuItem( "CopyAfter")
		  if me.SelStart >= me.Text.Len then
		    m.Enabled = False
		  end if
		  base.Append m
		  m = new MenuItem( "CopyAll")
		  if me.Text.Len <= 0 then
		    m.Enabled = False
		  end if
		  base.Append m
		End Function
	#tag EndEvent
	#tag Event
		Function ContextualMenuAction(hitItem as MenuItem) As Boolean
		  if hitItem.Text = "CopyAll" then
		    dim clp As new Clipboard
		    clp.Text = ReplaceLineEndings( me.Text, EndOfLine )
		    Return True
		  end if
		  if hitItem.Text = "CopyBefore" then
		    dim tmp As Integer = me.SelStart
		    me.SelStart = 0
		    me.SelLength = tmp
		    me.Copy
		    dim clp As new Clipboard
		    clp.Text = ReplaceLineEndings( clp.Text, EndOfLine )
		    Return True
		  end if
		  if hitItem.Text = "CopyAfter" then
		    me.SelLength = me.text.Len
		    me.Copy
		    dim clp As new Clipboard
		    clp.Text = ReplaceLineEndings( clp.Text, EndOfLine )
		    Return True
		  end if
		End Function
	#tag EndEvent
#tag EndEvents
